//
//  YHEffect.cpp
//  ThunderWarriors
//
//  Created by mac on 13-6-23.
//
//

#include "YHEffect.h"
#include "YHGameDefine.h"

bool YHEffect::IsResourceLoaded = false;

YHEffect::YHEffect(){
    
}

YHEffect::~ YHEffect(){
    
}

bool YHEffect::init(){
    return true;
}

void YHEffect::explode(CCNode* parent, CCPoint pos){
    if (! IsResourceLoaded) {
        LoadEffectResource();
    }
    
    CCSpriteFrame* pFrame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("explosion_01.png");
    CCSprite* explosion = CCSprite::createWithSpriteFrame(pFrame);
    parent->addChild(explosion, kExplosionZorder);
    explosion->setPosition(pos);
    CCAnimate* animate = CCAnimate::create(CCAnimationCache::sharedAnimationCache()->animationByName("Explosion"));
    CCCallFuncN* callFuncN = CCCallFuncN::create(this, callfuncN_selector(YHEffect::removeSprite));
    CCFiniteTimeAction* sequence = CCSequence::create(animate,callFuncN,NULL );
    explosion->runAction(sequence);
}

void YHEffect::LoadEffectResource(){
    if (IsResourceLoaded == false) {
        CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(s_explosion_plist);
        CCArray* animaFrames = CCArray::create();
        
        char str[80]={0};
        for (int i = 1; i < 36; i ++) {
            sprintf(str, "explosion_%02d.png",i);
            CCSpriteFrame* frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(str);
            animaFrames->addObject(frame);
        }
        CCAnimation* animation = CCAnimation::createWithSpriteFrames(animaFrames, 0.04);
        CCAnimationCache::sharedAnimationCache()->addAnimation(animation, "Explosion");
        IsResourceLoaded = true;
    }
}

void YHEffect::removeSprite(CCNode* pSender){
    CCSprite* sprite = (CCSprite*)pSender;
    if (sprite) {
        sprite->removeFromParentAndCleanup(true);
    }
}